package com.weikuang.fareast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.adapter.HotelAdapter;
import com.weikuang.fareast.bean.HotelBean;
import com.weikuang.fareast.http.MainHttp;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.ToastUtil;
import cn.dlc.commonlibrary.utils.rv_tool.RecyclerViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HotelActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.ed_number)
    EditText edNumber;
    @BindView(R.id.btn_goods_sure)
    Button btnGoodsSure;
    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;


    private HotelAdapter mHotelAdapter;
    HotelBean.DataBean mHoteldata = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hotel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);


        edNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }

            @Override
            public void afterTextChanged(Editable editable) {
                getUser();
            }
        });
        InitRecyclerView();
        getUser();
        btnGoodsSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent();
                i.putExtra("data",mHoteldata);
                setResult(4, i);
                finish();

            }
        });
    }

    private void InitRecyclerView() {
        mHotelAdapter = new HotelAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mHotelAdapter);

        mHotelAdapter.setOnItemClickListener(new HotelAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, HotelBean.DataBean data, int position) {
                edNumber.setText(data.title);
                mHoteldata = data;
            }
        });

    }

    private void getUser() {
        String keyword = edNumber.getText().toString();
        MainHttp.get().gethotelNew( keyword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<HotelBean>() {
                    @Override
                    public void onSuccess(HotelBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        mHotelAdapter.setNewData(hotelBean.data);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
}
